001 /* 002 * $RCSfile: RequestFactory.java,v $ 003 * 004 * Created on August 26, 2003, 9:55 AM 005 * 006 * This file is part of the STAR Scheduler. 007 * Copyright (c) 2002-2003 STAR Collaboration - Brookhaven National Laboratory 008 * 009 * STAR Scheduler is free software; you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as published by 011 * the Free Software Foundation; either version 2 of the License, or 012 * (at your option) any later version. 013 * 014 * STAR Scheduler is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with STAR Scheduler; if not, write to the Free Software 021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 022 */ 023 024 package gov.bnl.star.offline.scheduler; 025 026 import gov.bnl.star.offline.scheduler.*; 027 import java.io.*; 028 import java.util.*; 029 import java.util.logging.*; 030 import java.util.logging.Level; 031 import java.util.logging.Logger; 032 import javax.xml.parsers.*; 033 import javax.xml.transform.*; 034 import org.apache.xpath.*; 035 import org.w3c.dom.*; 036 import org.xml.sax.*; 037 import org.xml.sax.helpers.*; 038 039 /** 040 * 041 * @author carcassi 042 * @version $Revision: 1.3 $ $Date: 2004/02/12 18:55:12 $ 043 */ 044 public class RequestFactory { 045 static private Logger log = Logger.getLogger(RequestFactory.class.getName()); 046 047 /** Creates a new instance of RequestParser */ 048 public RequestFactory() { 049 } 050 051 private static Map defaults = new Hashtable(); 052 public static void setDefaults(Map defaults) { 053 RequestFactory.defaults = defaults; 054 } 055 056 public static Request parseXML(InputStream xml) { 057 log.finer("Start parsing"); 058 059 RequestXMLParser handler = new RequestXMLParser(); 060 handler.setDefaults(defaults); 061 062 // Use the default (non-validating) parser 063 SAXParserFactory factory = SAXParserFactory.newInstance(); 064 065 try { 066 // Parse the input 067 SAXParser saxParser = factory.newSAXParser(); 068 saxParser.parse(xml, handler); 069 } catch (Throwable t) { 070 log.log(Level.SEVERE, "Coulnd't parse job description", t); 071 throw new RuntimeException("Couldn't parse job description.\n" + 072 t.getMessage(), t); 073 } 074 075 Request[] requests = handler.getRequests(); 076 077 log.finer("Finished parsing"); 078 079 return requests[0]; 080 } 081 082 /** Tests the parser by analizing a file and displaying in the standard output the 083 * content of the Request list that the parser returned. 084 * @param args the first argument must be the name of the XML file containing the job 085 * description to be parsed 086 */ 087 public static void main(String[] args) { 088 // Enabling logging to the finest degree 089 Handler[] handlers = Logger.getLogger("").getHandlers(); 090 091 for (int index = 0; index < handlers.length; index++) { 092 handlers[index].setLevel(Level.FINEST); 093 } 094 095 LogManager.getLogManager().getLogger("").setLevel(Level.FINEST); 096 097 // TODO: Code migration, the code is for multiple requests 098 try { 099 Request[] jobs = new Request[] {parseXML(new FileInputStream(args[0]))}; 100 System.out.println("" + jobs.length + " were found in the XML file"); 101 102 for (int nJob = 0; nJob < jobs.length; nJob++) { 103 System.out.println("Job number " + (nJob + 1)); 104 System.out.println("Name : " + jobs[nJob].getName()); 105 System.out.println("Description : " + jobs[nJob].getDescription()); 106 System.out.println("User : " + jobs[nJob].getUsername()); 107 System.out.println("Command : " + jobs[nJob].getCommand()); 108 System.out.println("Standard in : " + jobs[nJob].getStdIn()); 109 System.out.println("Standard out : " + jobs[nJob].getStdOut()); 110 111 System.out.println("Requested input file list:"); 112 113 List list = jobs[nJob].getInputList(); 114 System.out.println(" Number of input : " + list.size()); 115 116 for (int nInput = 0; nInput < list.size(); nInput++) { 117 System.out.println(" " + list.get(nInput)); 118 } 119 120 System.out.println("Requested output file list:"); 121 122 List oList = jobs[nJob].getOutputList(); 123 System.out.println(" Number of output : " + oList.size()); 124 125 for (int nOutput = 0; nOutput < oList.size(); nOutput++) { 126 System.out.println(" " + oList.get(nOutput)); 127 } 128 129 } 130 } catch (Exception e) { 131 System.out.println("Couldn't load"); 132 e.printStackTrace(); 133 } 134 } 135 }